#include <stdio.h>
#include <stdlib.h>

typedef struct book {
	char str[30];
	int price;
} Book;

int cmd(const void *a, const void *b) {
	Book *c = NULL, *d = NULL;
	c = (Book *)a;
	d = (Book *)b;
	return c->price - d->price;
}

int main() {
	int n, i;
	scanf("%d", &n);
	Book *B = (Book *)malloc(sizeof(Book) * n);
	for (i = 0; i < n; i++) {
		scanf("%s %d", B[i].str, &B[i].price);
	}

	qsort(B, n, sizeof(Book), cmd);//

	for (i = 0; i < n; i++) {
		printf("%s\n", B[i].str);
	}

}